using System;
using System.Collections.Generic;
using System.Text;
//using System.Windows.Forms;

namespace Tarea5
{
    class Controller : ControllerInterface
    {
        //Just one model reference (one model at a time)
        private ModelInterface model;
        //Multiple views at any given time...
        private List<ViewInterface> views = new List<ViewInterface>();
        //Model titles...
        private string[] titles = { "Tic Tac Toe Interactive With Random Movements", "Tic Tac Toe Interactive With Smart Random Movements", "Tic Tac Toe Interactive With Minimax", "Tic Tac Toe Interactive With AlfaBeta Prunning" };
        private string defaultTitle = "Tic Tac Toe Interactive";

        public Controller() { }

        public void SetModel(Behaviors.Behavior behavior, int sleepInterval, short [,] initialBoard, bool HumanMovesFirst, int size, int depth)// JF
        {
            //Choose model based on desired behavior
            int i = 0;
            if (behavior == Behaviors.Behavior.TTTHRMC) // JF
            {
                model = new ModelTTTHRMC();
            }
            else if (behavior == Behaviors.Behavior.TTTHSRMC)
            {
                model = new ModelTTTHSRMC();
                i = 1;
            }
            else if (behavior == Behaviors.Behavior.TTTHSC)
            {
                model = new ModelTTTHSC();
                i = 2;
            }
            else if (behavior == Behaviors.Behavior.TTTHCAB)
            {
                model = new ModelTTTHCAB();
                i = 3;
            }
  
            //initialize desired model...
            model.Initialize(sleepInterval, size, HumanMovesFirst);

            if (behavior == Behaviors.Behavior.TTTHSC)
                ((ModelTTTHSC)model).Depth = depth;

            if (behavior == Behaviors.Behavior.TTTHCAB)
                ((ModelTTTHCAB)model).Depth = depth;

            //notify views that the model has been changed...
           foreach (ViewInterface view in views)
                view.SetModel(titles[i], model);
        } 

        public int MoveCell(int row, int col)
        {
            return model.MoveCell(row, col);
        }

        public int UnDoMoveCell()
        {
            return model.UnDoMoveCell();
        }

        public void Start()
        {
            model.Start();
        }

        public void Pause()
        {
            model.Pause();
        }

        public void Stop()
        {
            model.Stop();
        }

        public void SetSleepInterval(int sleepInterval)
        {
            model.SleepInterval = sleepInterval;
        }

        public bool IsRunning()
        {
            return model.IsRunning;
        }

        public bool IsStopped()
        {
            return model.IsStopped;
        }

        public bool IsPaused()
        {
            return model.IsPaused;
        }

        public void RegisterView(ViewInterface o)
        {
            views.Add(o);
            o.Initialize(defaultTitle, this);
        }

        public void RemoveView(ViewInterface o)
        {
            views.Remove(o);
        }
    }
}
